package org.fightingliu.demo.service.impl;

import co.elastic.clients.elasticsearch._types.query_dsl.DateRangeQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import org.fightingliu.demo.repository.es.entity.User;
import org.fightingliu.demo.repository.es.repository.UserRepository;
import org.fightingliu.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.RefreshPolicy;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.CriteriaQueryBuilder;
import org.springframework.data.elasticsearch.core.query.IndexBoost;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : LiuChao
 * @description : TODO
 * @date : 2025-04-13 18:36:40
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserRepository userReponsitory;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    private ElasticsearchOperations elasticsearchOperations;

    @Override
    public String save(User user) {
        User save = userReponsitory.save(user);
        return save.getId();
    }

    @Override
    public User getById(String id) {
        User users = userReponsitory.queryUserByUserId(id);
        return users;
    }

    @Override
    public String update(User user) {
        userReponsitory.save(user, RefreshPolicy.IMMEDIATE);
        return JSONObject.toJSONString(userReponsitory.save(user, RefreshPolicy.IMMEDIATE));
    }

    @Override
    public String delete(String id) {
        userReponsitory.deleteUserByUserId(id);
        return "ok";
    }

    public List<User> query(String id) {

        NativeQuery query =  NativeQuery.builder()
                .withFilter(f->f.match(m -> m
                        .field("userName").query("吴六")
                        .field("userId").query("1005")))
                .withQuery(q->q.range(r->r.date(new DateRangeQuery.Builder().field("birthday").gte("1998-09-10").lt("1998-11-11").build())))
                .build();
        return elasticsearchTemplate.search(query,User.class, IndexCoordinates.of("user*")).stream()
                .map(SearchHit::getContent)
                .collect(Collectors.toList());
    }


}
